/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Managers;

import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioNode.Status;
import com.jme3.audio.AudioRenderer;
import com.jme3.math.Vector3f;
import mygame.model.Game;

/**
 *
 * @author tommy
 */
public class SoundManager {
    private static AudioRenderer audioRenderer = null;
    private static AssetManager assetManager = null;

    private static AudioNode fallSound = null;
    private static AudioNode walkSound = null;
    private static AudioNode pickUpSound = null;
    private static AudioNode gameOverSound = null;
    private static AudioNode damageSound = null;
    private static AudioNode deathSound = null;
    private static AudioNode jumpSound = null;
    private static AudioNode ambientMusic = null;
    private static Float soundVolume = null;
    private static Float musicVolume = null;
    private static Game game;

    public static void initSoundManager(Game game) {
        SoundManager.audioRenderer = game.getAudioRenderer();
        SoundManager.assetManager = game.getAssetManager();
        SoundManager.game = game;
        setMusicVolume(0.5f);
        setSoundVolume(0.5f);
    }

    public static void playWalkSound() {
      //  System.out.println("playing walk sound...");
        try {
            if (walkSound == null) {
//                walkSound = new AudioNode(assetManager, "Sounds/Player/die1.wav");
                walkSound.setLooping(false);
                //walkSound.setPositional(true);
                //walkSound.setLocalTranslation(Vector3f.ZERO.clone());
                walkSound.setVolume(soundVolume);
            }

            if (walkSound.getStatus() != Status.Playing) {
                audioRenderer.playSource(walkSound);
            }
            
            /*if(menuBGMSound.getStatus() != Status.Playing && Preferences.getInstance().isSoundFXEnabled()) {
                audioRenderer.playSource(stepsSound);
            }
             *
             */

        } catch (Exception e) {
       //     System.out.println("Walk Sound file not found");
        }
    }

    public static void playJumpSound() {
        try {
             if (jumpSound == null) {
//                jumpSound = new AudioNode(assetManager, "Sounds/Player/jump.wav");
                jumpSound.setLooping(false);
                jumpSound.setVolume(soundVolume / 10);
            }

            if (jumpSound.getStatus() == Status.Playing) {
                audioRenderer.stopSource(jumpSound);
            }
            audioRenderer.playSource(jumpSound);


        } catch (Exception e) {
            System.out.println("Jump sound file not found");
        }
    }

    public static void playPickUpSound() {
        try {
            if (pickUpSound == null) {
//                pickUpSound = new AudioNode(assetManager, "Sounds/Items/PickUpItem.wav");
                pickUpSound.setLooping(false);
                pickUpSound.setVolume(soundVolume / 10);
            }

            if(pickUpSound.getStatus() != Status.Playing) {
                audioRenderer.playSource(pickUpSound);
            }

        } catch (Exception e) {
            System.out.println("Pick up sound file not found");
        }
    }

    public static void playFallSound() {
        try {
            if (fallSound == null) {
//                fallSound = new AudioNode(assetManager, "Sounds/Player/fall.wav");
                fallSound.setLooping(false);
                fallSound.setVolume(soundVolume / 10);
            }

            if(fallSound.getStatus() != Status.Playing) {
                audioRenderer.playSource(fallSound);
            }

        } catch (Exception e) {
            System.out.println("Fall Sound file not found");
        }
    }

    public static void playDamageSound() {
        try {
            if (damageSound == null) {
//                damageSound = new AudioNode(assetManager, "Sounds/Player/damage.wav");
                damageSound.setLooping(false);
                damageSound.setVolume(soundVolume / 10);
            }

            if(damageSound.getStatus() != Status.Playing) {
                audioRenderer.playSource(damageSound);
            }

        } catch (Exception e) {
            System.out.println("Damage Sound file not found");
        }
    }

    public static void playDeathSound() {
        try {
            if (deathSound == null) {
//                deathSound = new AudioNode(assetManager, "Sounds/Player/death.wav");
                deathSound.setLooping(false);
                deathSound.setVolume(soundVolume / 10);
            }

            if(deathSound.getStatus() != Status.Playing) {
                audioRenderer.playSource(deathSound);
            }

        } catch (Exception e) {
            System.out.println("Death Sound file not found");
        }
    }

    public static void playGameOverSound() {
        try {
            if (gameOverSound == null) {
//                gameOverSound = new AudioNode(assetManager, "Sounds/General/gameOver.wav");
                gameOverSound.setLooping(false);
                gameOverSound.setVolume(musicVolume / 10);
            }

            if(gameOverSound.getStatus() != Status.Playing) {
                audioRenderer.playSource(gameOverSound);
            }

        } catch (Exception e) {
            System.out.println("Game over Sound file not found");
        }
    }

    public static void initAmbientMusic() {
        try {
            if (ambientMusic == null) {
//                ambientMusic = new AudioNode(assetManager, "Sounds/Ambient/Birds1.wav");
                ambientMusic.setLooping(false);
                ambientMusic.setPositional(true);
                ambientMusic.setLocalTranslation(Vector3f.ZERO.clone());
                ambientMusic.setVolume(musicVolume);
                game.getAudioRenderer().playSource(ambientMusic); // play continuously!
            }
        } catch (Exception e) {
             System.out.println("Ambient Sound file not found");
        }
    }
    
    public static void stopWalkSound() {
        if (walkSound != null && walkSound.getStatus() == Status.Playing) audioRenderer.stopSource(walkSound);
    }
    
    public static void stopAmbientMusic() {
        if (ambientMusic != null && ambientMusic.getStatus() == Status.Playing) audioRenderer.stopSource(ambientMusic);
    }
    
    public static void setSoundVolume(float volume) {
        SoundManager.stopWalkSound();

        soundVolume = volume;

        walkSound = null;
        damageSound = null;
        fallSound = null;
        pickUpSound = null;
        jumpSound = null;
    }
    

    public static void setMusicVolume(float volume) {
        
        musicVolume = volume;
        gameOverSound = null;
        ambientMusic = null;
    }
}
